package com.yhsoft.common.response;

import com.baomidou.mybatisplus.core.metadata.IPage;
import lombok.Data;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author: yyyyyyyh
 * @create: 2023-09-24
 * @Version 1.0
 **/
@Data
public class PageResult<T> {
    private long total;
    private long pageIndex;
    private long pageSize;
    private long totalPages;
    private List<T> rows;

    public PageResult(long total, long pageIndex, long pageSize, long totalPages, List<T> data) {
        this.total = total;
        this.pageIndex = pageIndex;
        this.pageSize = pageSize;
        this.totalPages = totalPages;
        this.rows = data;
    }

    public PageResult() {
    }

    public static <T, E> PageResult<T> valueOf(IPage<E> pageData, PageDataParseMethod<E, T> parseMethod) {
        List<T> ret = pageData.getRecords().stream().map(parseMethod::parse).collect(Collectors.toList());
        return new PageResult<>(pageData.getTotal(), pageData.getCurrent(), pageData.getSize(), pageData.getPages(), ret);
    }

    public static <T> PageResult<T> valueOf(IPage<T> pageData) {
        return new PageResult<>(pageData.getTotal(), pageData.getCurrent(), pageData.getSize(), pageData.getPages(), pageData.getRecords());
    }

    @FunctionalInterface
    public interface PageDataParseMethod<E, T> {
        T parse(E e);
    }
}
